package com.example.cmliu.fragmentdemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

public class DetailFragment extends Fragment {

    // 创建一个DetailFragment的新实例，其中包括要传递的数据包
    public static DetailFragment newInstance(int index) {
        DetailFragment f = new DetailFragment();

        Bundle bundle = new Bundle();
        bundle.putInt("index", index);
        f.setArguments(bundle);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }

        View view = inflater.inflate(R.layout.detail, container, false);
        TextView txtView = (TextView) view.findViewById(R.id.txt_detail);
        txtView.setText(Data.DETAIL[getShownIndex()]);

        return view;
    }

    public int getShownIndex() {
        return getArguments().getInt("index", 0);
    }
}
